package exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import exam.bean.PaperInfo;
import exam.bean.UserInfo;
import exam.bean.UtilFiles;
import exam.dao.PaperInfoDao;
import exam.dao.UserInfoDao;
import exam.dao.UtilFileDao;
import exam.service.IPaperInfoService;
import exam.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PaperInfoServiceImpl implements IPaperInfoService {
    @Autowired
    PaperInfoDao paperInfoDao;

    @Autowired
    UtilFileDao utilFileDao;

    @Autowired
    UserInfoDao userInfoDao;


    @Override
    public int insertInfo(PaperInfo paperInfo,String upLoadPath) {
        paperInfo.setEnterTime(new Date());
        paperInfo.setFlag(1);
        int paperRe = paperInfoDao.insertInfo(paperInfo);
        int fileRe = 0;
        if(paperRe > 0 && paperInfo.getFiles() != null){
            //多文件上传
            for (MultipartFile file:paperInfo.getFiles()) {
                String fileURL = FileUtil.upLoadFileSingle(file,upLoadPath);
                UtilFiles files = new UtilFiles();
                files.setDataId(paperInfo.getId());
                files.setUtilName(fileURL);
                files.setUtilAddress(upLoadPath);
                files.setUtilSrcName(file.getOriginalFilename());
                files.setUtilFileType(file.getContentType());
                files.setUtilType(2);
                fileRe += utilFileDao.insertInfo(files);
            }
        }
        return fileRe + paperRe;
    }

    @Override
    public PageInfo<PaperInfo> selectInfo(int pageSize, int pageNum,Map<String,Object> map) {

        UserInfo userInfo = getCurrentUserInfo();
        //查询属于自己的数据 ---> 管理员除外
        if(userInfo.getUserGroupId() != 1 ){
            map.put("enterUserId",userInfo.getId());
        }

        PageHelper.startPage(pageSize,pageNum);
        return new PageInfo<>(paperInfoDao.selectInfo(map));
    }

    @Override
    public int updateInfo(PaperInfo paperInfo,String upLoadPath) {
        //多文件上传
        Integer fileRe = 0;
        if(paperInfo.getFiles() != null){
            for (MultipartFile file:paperInfo.getFiles()) {
                String fileURL = FileUtil.upLoadFileSingle(file,upLoadPath);
                UtilFiles files = new UtilFiles();
                files.setDataId(paperInfo.getId());
                files.setUtilName(fileURL);
                files.setUtilAddress(upLoadPath);
                files.setUtilSrcName(file.getOriginalFilename());
                files.setUtilFileType(file.getContentType());
                files.setUtilType(2);
                fileRe += utilFileDao.insertInfo(files);
            }
        }
        return paperInfoDao.updateInfo(paperInfo) + fileRe;
    }

    @Override
    public int deleteInfo(int id) {
        return paperInfoDao.deleteInfo(id);
    }

    @Override
    public PaperInfo selectInfoSingle(Map<String, Object> map) {
//        附件查询
        PaperInfo paperInfo = paperInfoDao.selectInfo(map).get(0);
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("utilType",2);
        queryMap.put("dataId",map.get("id"));
        paperInfo.setUtilFiles(utilFileDao.selectInfo(queryMap));

        return paperInfo;
    }


    public UserInfo getCurrentUserInfo(){
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        Map<String, Object> map = new HashMap<>();
        map.put("name",authenticationToken.getPrincipal());
        return userInfoDao.selectInfo(map).get(0);
    }
}
